//
//  DownloadCell.m
//  DMT_VR
//
//  Created by xin on 2016/11/21.
//  Copyright © 2016年 Elephant-Parade. All rights reserved.
//

#import "DownloadCell.h"
#import "Masonry.h"

#import "VedioModel.h"
#import "UIImageView+YYWebImage.h"
#import "HSDownloadManager.h"


#define WHC_BackgroundDownload (0)

@interface DownloadCell()
{
    BOOL                           _hasDownloadAnimation;
}
@property (strong, nonatomic) UIImageView *backgroundImageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *downloadLabel;
@property (strong, nonatomic) UIButton *downloadButton;

@end

@implementation DownloadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _backgroundImageView = [UIImageView new];
        _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backgroundImageView.clipsToBounds = YES;
        [self.contentView addSubview:_backgroundImageView];
        
        UIView * markView = [UIView new];
        markView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [self.contentView addSubview:markView];
        
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0];
        _titleLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleLabel];
        
        _downloadLabel = [UILabel new];
        _downloadLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14.0];
        _downloadLabel.textColor = [UIColor whiteColor];
        [self.contentView addSubview:_downloadLabel];
        
        _downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downloadButton setImage:[UIImage imageNamed:@"download_pause"] forState:UIControlStateNormal];
        [_downloadButton setImage:[UIImage imageNamed:@"download_play"] forState:UIControlStateSelected];
        [self.contentView addSubview:_downloadButton];
        [_downloadButton addTarget:self action:@selector(clickDownload:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        [markView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_backgroundImageView).multipliedBy(1);
            make.centerX.equalTo(_backgroundImageView);
        }];
        
        [_downloadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).with.offset(16);
            make.centerX.equalTo(_backgroundImageView);
        }];
        
        [_downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_downloadLabel.mas_bottom).with.offset(16);
            make.centerX.equalTo(_backgroundImageView);
            make.width.equalTo(@50);
            make.height.equalTo(@50);
        }];
    }
    return self;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)clickDownload:(UIButton *)sender
{
    [[HSDownloadManager sharedInstance] download:self.model.vediourl progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString * value = [NSString stringWithFormat:@"%.f%%",progress * 100];
            _downloadLabel.text = value;
        });
    } state:^(DownloadState state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.downloadLabel.alpha = ((state == DownloadStateCompleted) ? 0:1);
            self.downloadButton.alpha = ((state == DownloadStateCompleted) ? 0:1);
            
            if (state == DownloadStateStart) {
                self.downloadButton.selected = NO;
            }else
            {
                self.downloadButton.selected =YES;
            }
        });
    }];
}

- (void)setModel:(VedioModel *)model
{
    _model = model;
    
    _titleLabel.text = [NSString stringWithFormat:@"%@  %@",model.title_ch,model.title_en];
    
    //设置背景图
    NSURL * imagePath = [NSURL URLWithString:model.imgurl];
    [self.backgroundImageView
     setImageWithURL:imagePath
     placeholder:[UIImage imageNamed:@"placeholder_img"]
     options:YYWebImageOptionSetImageWithFadeAnimation
     completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
         
     }];
    
    [[HSDownloadManager sharedInstance] wxDownload:model.vediourl progress:^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString * value = [NSString stringWithFormat:@"%.f%%",progress * 100];
            _downloadLabel.text = value;
        });
    } state:^(DownloadState state) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"state=%d",state);
            self.downloadLabel.alpha = ((state == DownloadStateCompleted) ? 0:1);
            self.downloadButton.alpha = ((state == DownloadStateCompleted) ? 0:1);
            
            if (state == DownloadStateStart) {
                self.downloadButton.selected = NO;
            }else
            {
                self.downloadButton.selected =YES;
            }
        });
    }];
}

@end
